package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.ComplaintDao;
import com.wdy.po.Complaint;
import com.wdy.po.Tenement;
import com.wdy.service.ComplaintManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings("unchecked")
public class ComplaintManagerImpl implements ComplaintManager {

	private ComplaintDao complaintDao;

	public void setComplaintDao(ComplaintDao complaintDao) {
		this.complaintDao = complaintDao;
	}

	public void saveComplaint(Complaint complaint) {
		complaintDao.save(complaint);
	}

	public void removeComplaint(Complaint complaint) {
		complaintDao.delete(complaint);
	}

	@Override
	public void removeComplaints(Object[] objects) {
		// TODO Auto-generated method stub
		complaintDao.deleteBatch(objects);
	}

	public void updateComplaint(Complaint complaint) {
		complaintDao.attachDirty(complaint);
	}

	public Complaint getComplaint(Integer id) {
		return complaintDao.findById(id);
	}

	public List<Complaint> listAll() {
		return complaintDao.findAll();
	}

	public List<Complaint> findByExample(Complaint complaint) {
		return complaintDao.findByExample(complaint);
	}

	public List<Complaint> findByDate(String value) {
		return complaintDao.findByDate(value);
	}

	public List<Tenement> findTenement(Object value) {
		return complaintDao.findTenement(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = complaintDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) complaintDao.query(conList, page);
		List<Complaint> complaints = new LinkedList<Complaint>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Complaint) {
						complaints.add((Complaint) oo);
					}
				}
			} else {
				if (o instanceof Complaint) {
					complaints.add((Complaint) o);
				}
			}
		}
		return new Result(page, complaints);
	}

}
